<?php
namespace app\manage\controller;
use app\manage\controller\BaseController;
use app\manage\model\AdImg;
use think\facade\Db;
use think\facade\View;
use app\manage\model\Advertisement as Advertisementmodel;

class Advertisement extends BaseController
{
    public function index()
    {
    	$advertisement=new Advertisementmodel();
    	
        $key=input('key','');
        View::assign('key',$key);
        $advertisement=$advertisement->where(function($query) use ($key){
                $query->whereor('key','like','%'.$key.'%');
            })->order('id desc')->paginate(10,false,['query'=>request()->param()]);
        View::assign('advertisement',$advertisement);
        View::assign('count1', $advertisement->total());
       	return View::fetch();
    }
	public function ajax()
    {
    	$data=input('post.');
		$advertisement=new Advertisementmodel();
		$ad_img=new AdImg();
		if($data['type']=='advertisement_del'){
			$id=$data['id'];
            $ad_img->where('ad_id',$id)->delete();
			$info=$advertisement->destroy($id);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='advertisement_del_img'){
			if(AdImg::destroy($data['id'])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }

	public function add_img()
	{
		if(request()->isPost()){
			$data=input('post.');
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
			$file = request()->file('');
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'advertisement', $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }

			}else{
				$this->error('图片必须填写');
			}
			$link=new AdImg();
			$link->data($data);
			$res=$link->save();
			if($res){
				$this->success('添加成功',url('advertisement/index',['st'=>1]));
			}else{
				$this->error('添加失败了');
			}
		}
        View::assign('ad_id', input('id'));
		return View::fetch();
	}
	public function guan_img()
	{
		$data=(new AdImg())->where('ad_id',input('id'))->order('id desc')->paginate(8);
        View::assign('data',$data);
        View::assign('count1', $data->total());
        View::assign('ad_id', input('id'));
	   	return View::fetch();
	}
	public function img_edit()
	{
		if(request()->isPost()){
			$data=input('post.');
			
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
			$file = request()->file('');
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'advertisement', $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
			}
			$advertisement=new AdImg();
			$res=$advertisement->update($data,['id' => input('id')]);
			if($res){
				$this->success('修改成功',url('advertisement/index',['st'=>1]));
			}else{
				$this->error('修改失败了');
			}
		}
		$id=input('id');
		$data= Db::name('ad_img')->find($id);
		View::assign('data',$data);
	   return View::fetch();
	}

	public function add()
    {
    	if(request()->isPost()){
			$data=input('post.');
			$validate = new \app\manage\validate\Advertisement;
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
			
			$file = request()->file('');
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'advertisement', $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
			}
			$link=new Advertisementmodel();
			$link->data($data);
			$res=$link->save();
			if($res){
				return $this->success('添加成功',url('advertisement/index',['st'=>1]));
			}else{
				$this->error('添加失败了');
			}
    	}
       return View::fetch();
    }
	public function edit()
    {
    	$advertisement = new Advertisementmodel();
    	if(request()->isPost()){
    		$data=input('post.');
			$validate = new \app\manage\validate\Advertisement;
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}

			$file = request()->file();
			if(isset($file['img'])){
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'advertisement', $file['img']);
                    $data['img']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
			}
			$res=$advertisement->update($data,['id' => input('id')]);
			if($res){
				$this->success('修改成功',url('advertisement/index',['st'=>1]));
			}else{
				$this->error('修改失败了');
			}
    	}
		$id=input('id');
		$data= $advertisement->find($id);
        View::assign('data',$data);
       return View::fetch();
    }
}
